<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <!-- 搜索表单 -->
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="编号" prop="id">
            <el-input
              v-model="queryParams.id"
              placeholder="请输入类别编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
     
          <el-form-item label="类别" prop="categoryName">
            <el-select v-model="queryParams.categoryName" placeholder="请选择">
              <el-option
                v-for="item in categoryOptions"
                :key="item.id"
                :label="item.categoryName"
                :value="item.categoryName"
                @keyup.enter.native="handleQuery"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
        <!-- 数据操作按钮 -->
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="small"
              @click="handleAdd"
              v-hasAuthority="['pos:category:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="small"
              :disabled="single"
              v-hasAuthority="['pos:category:edit']"
              @click="handleUpdate"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="small"
              :disabled="single"
              @click="handleDelete"
              v-hasAuthority="['pos:category:delete']"
              >删除</el-button
            >
          </el-col>
        </el-row>

        <el-table
          :data="categoryList"
          v-loading="loading"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column
            prop="id"
            label="类别编号"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="categoryName"
            label="类别名称"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="createTime"
            label="创建日期"
            align="center"
          ></el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="small"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >修改</el-button
              >
              <el-button
                size="small"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类别名称" prop="categoryName">
          <el-input v-model="form.categoryName" placeholder="请输入名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 分页加载 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.current"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import {
  listCategorytByPage,
  delCategory,
  addCategory,
  updateCategory,
} from "@/api/category";
export default {
  name: "categoryView",
  data() {
    return {
      categoryName:null,
      loading: true,
      // 类别数据
      categoryList: [],
      // 分页数据
      queryParams: {
        current: 1,
        id: "",
        pageSize: 10,
        sortField: "create_time",
        sortOrder: "ascend",
        categoryName: "",
      },
      // 选项
      categoryOptions: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单
      form: {},

      rules: {
        categoryName: [
          { required: true, message: "类别名称不能为空", trigger: "blur" },
        ],
      },
      // 总条数
      total: 0,
    };
  },
  created() {
    this.getList();
    this.getCategoryList();
  },
  methods: {
    /** 处理分页 */
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.queryParams.current = val;
      this.getList();
    },
    /** 表单重置 */
    resetForm() {
      // 可进行数据初始化工作
      this.form = {
        categoryName: "",
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm();
      this.open = true;
      this.title = "添加数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm();
      this.categoryName = row.categoryName
      // const categoryId = row.id || this.ids;
      this.form = row;
      this.open = true;
      this.title = "修改数据";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.createTime != null) {
            delete this.form.createTime;
            if(this.categoryName==this.form.categoryName){
              delete this.form.categoryName
            }
            updateCategory(this.form).then((response) => {
              this.getList();
              this.cancel();
              this.$message.success("修改成功");
            });
          } else {
            addCategory(this.form).then((response) => {
              this.getList();
              this.cancel();
              this.$message.success("新增成功");
            });
          }
        }
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.title = "";
      this.resetForm();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const categoryId = row.id || this.ids;

      this.$confirm(`是否确认删除类别编号为 ${categoryId} 的数据项?`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delCategory({ id: categoryId });
        })
        .then(() => {
          this.getList(); // 刷新数据
          this.$message.success("删除成功");
        })
        .catch(() => {});
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.getList();
    },
    // 获取全部类别
    getCategoryList() {
      let queryParams = {
        current: 1,
        id: "",
        pageSize: 5,
        sortField: "create_time",
        sortOrder: "ascend",
        categoryName: "",
      };
      listCategorytByPage(queryParams).then((response) => {
        this.categoryOptions = response.data.records;
      });
    },
    // 根据需求获取类别数据
    getList() {
      this.loading = true;
      listCategorytByPage(this.queryParams).then((response) => {
        this.categoryList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
      this.getCategoryList();
    },
  },
};
</script>
<style scoped>
</style>